// pages/start/start.js
const app = getApp()
Page({
  

  /**
   * 页面的初始数据
   */
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    is_login: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 查看是否授权
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: function (res) {
              //从数据库获取用户信息
              that.queryUsreInfo();
              //用户已经授权过
              wx.switchTab({
                url: '/pages/index/index'
              })
            }
          });
        }
      }
    })

  },

  onGotUserInfo: function (e) {

    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      var that = this;
      //插入登录的用户的相关信息到数据库
      getApp().globalData.userInfo = e.detail.userInfo;      
      wx.request({
        url: 'https://www.shencheyi.com/api/v1//User/UpUserInfo?userId=' + app.globalData.userId + '&headPortrait=' + e.detail.userInfo.avatarUrl + '&nickName=' + e.detail.userInfo.nickName + '&sex=' + e.detail.userInfo.gender,
        data: {
        },
        method: 'PUT',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Accept': 'application/json',
        }, // 默认值
        success(res) {
          that.queryUsreInfo();
          console.log(res,"插入小程序登录用户信息成功！");
          app.globalData.userInfo = e.detail.userInfo;
          console.log('userInfo', app.globalData.userInfo)
          //用户已经授权过
          wx.navigateTo({
            url: '/pages/login/login'
          })

        }
      })
      //授权成功后，跳转进入小程序首页
    } else {
      //用户按了拒绝按钮
      wx.showModal({
        title: '警告',
        content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
        showCancel: false,
        confirmText: '返回授权',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击了“返回授权”')
          }
        }
      })
    }
  },
  
  queryUsreInfo: function () {

    wx.request({
      url: 'https://www.shencheyi.com/api/v1//User/UserInfo',
      data: {
        userId: app.globalData.userId
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log('queryUsreInfo', res)
      
      }
    }) 
  },

})